﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using ScrumTable.DL.Data.Common;
using ScrumTable.DL.Data.Generic;
using ScrumTable.DL.Data.Generic.Services;
using ScrumTable.DL.Data.Local.Config.Driver;
using ScrumTable.DL.Data.Local.Services;

#endregion

namespace ScrumTable.DL.Data.Local
{
    /// <summary>
    /// Contains all data used by the data layer, this is the entry
    /// point for the data layer and contains all required objects in
    /// order to connect to a data source.
    /// </summary>
    public class DataContext : GenericDataContextBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly IDataSaver _dataSaver;
        private readonly IDataLoader _dataLoader;

        private IDataSaveManagerFactory _dataSaveManagerFactory = new NullDataSaveManagerFactory();

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the data provider instance.
        /// </summary>
        internal DataProvider DataProvider { get; set; }

        /// <summary>
        /// Gets the driver configuration of the current local driver instance.
        /// </summary>
        internal DriverConfig ConfigurationInternal { get { return (DriverConfig)Configuration; } }

        /// <summary>
        /// Gets the internal data loader instance.
        /// </summary>
        protected override IDataLoader DataLoader
        {
            get { return _dataLoader; }
        }

        /// <summary>
        /// Gets the internal data saver instance.
        /// </summary>
        protected override IDataSaver DataSaver
        {
            get { return _dataSaver; }
        }

        /// <summary>
        /// Gets the data save manager factory which is used in conjunction
        /// with the connect procedure.
        /// </summary>
        protected override IDataSaveManagerFactory DataSaveManagerFactory
        {
            get { return _dataSaveManagerFactory; }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DataContext instance. This constructor must be
        /// public because it's being created by the ScrumTable base library.
        /// </summary>
        public DataContext(IDataContextInfo driverInfo)
            : base(driverInfo)
        {
            InitDataSaveManagerFactory();
 
            _dataSaver = new DataSaver();
            _dataLoader = new DataLoader();
        }
        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates the new data connection instance which is used to connect to the
        /// underlying data storage.
        /// </summary>
        /// <returns>Returns the newly created instance.</returns>
        protected override DataConnectionBase CreateDataConnection()
        {
            return new DataConnection(this);
        }

        private void InitDataSaveManagerFactory()
        {
            if (ConfigurationInternal.SaveEnabled)
            {
                _dataSaveManagerFactory = new DataSaveManagerFactory();
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}